package ydzhao.weixin.tuisong.util;

import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import ydzhao.weixin.tuisong.common.UserCom;

import java.util.Map;

public class Pusher {

    private static Map<String,String> mapData = GuessMi.getGuessMi();

    /**
     * 早上好
     * @param openId
     */
    public static void push(String openId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(UserCom.APPID);
        wxStorage.setSecret(UserCom.SECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //模版id
        String templateId = "RfMNP0QZhzdTqYd3JULqh4VM0JDBACKQBPRqEESpxJ4";
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                //.url("https://30paotui.com/")//点击模版消息要访问的网址
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
//        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        //填写变量信息，比如天气之类的
        Map<String, Object> today = Tianqi.getNanjiTianqi(openId);
        JSONObject value = (JSONObject) today.get("today");
        JSONObject location = (JSONObject) today.get("location");

        templateMessage.addData(new WxMpTemplateData("riqi", value.get("date")+ "  "+ value.get("week"),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("ip",location.get("city").toString(),"#145DBF"));
        templateMessage.addData(new WxMpTemplateData("tianqi",value.get("text_day").toString(),"#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("low",value.get("low") + "","#173177"));
        templateMessage.addData(new WxMpTemplateData("high",value.get("high")+ "","#173177" ));
        templateMessage.addData(new WxMpTemplateData("caihongpi",mapData.get("content"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#7BE1E1"));
        templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#0969DA"));
        templateMessage.addData(new WxMpTemplateData("jinju", GuessMi.getJinJu()+"","#C71585"));
        templateMessage.addData(new WxMpTemplateData("tips","答案将在中午12点推送","#FF69B4"));
        //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
//        templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));
        String beizhu = "";
//        if(JiNianRi.getJieHun() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
        if(JiNianRi.getLianAi() % 365 == 0){
            beizhu = "今天是在一起的周年纪念日！";
        }
//        if(JiNianRi.getLinZhen() % 365 == 0){
//            beizhu = "今天是结婚纪念日！";
//        }
        templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));


        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 答案小助手
     * @param openId
     */
    public static void pushAnswer(String openId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(UserCom.APPID);
        wxStorage.setSecret(UserCom.SECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //模版id
        String templateId = "FXN7Oz0fTXI-O9jW7crhmtsneFuQftOwAdFVc5c_0OY";
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
//        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("caihongpi",mapData.get("content"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("answer",mapData.get("answer"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("reason",mapData.get("reason"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("ps","宝~愿你每天好心情~","#FF1493"));

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 更新每天字谜
     */
    public static void update(){
        mapData = GuessMi.getGuessMi();
    }

}
